package org.wdcode.web.memcache.client;

import org.wdcode.web.memcache.need.MemCacheNeed;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

/**
 * MemCached的客户端调用实现
 * @author WD
 * @since JDK6
 * @version 1.0 2010-08-29
 */
final class MemCacheClientImpl implements MemCacheClient {
	// MemCache客户端
	private MemCachedClient	client;
	// SockIO连接池
	private SockIOPool		pool;
	// MemCacheNeed
	private MemCacheNeed	need;

	/**
	 * 私有构造
	 */
	public MemCacheClientImpl(MemCacheNeed need) {
		// 设置MemCacheNeed
		this.need = need;
		// 实例化client
		client = new MemCachedClient();
		// 获得池
		pool = SockIOPool.getInstance();
		// 设置服务器
		pool.setServers(need.getServers());
		// 设置权重列表
		pool.setWeights(need.getWeights());
		// 设置初始化连接
		pool.setInitConn(need.getInitConn());
		// 设置最小连接
		pool.setMinConn(need.getMinConn());
		// 设置最大连接
		pool.setMaxConn(need.getMaxConn());
		// 设置空闲时间
		pool.setMaxIdle(need.getMaxIdle());
		// 设置睡眠时间
		pool.setMaintSleep(need.getMaintSleep());
		// 设置超时读取
		pool.setSocketTO(need.getSocketTO());
		// 设置连接超时
		pool.setSocketConnectTO(need.getSocketConnectTO());
		// 池初始化
		pool.initialize();
	}

	/**
	 * 获得MemCachedClient
	 * @return MemCachedClient
	 */
	public final MemCachedClient getClient() {
		return client;
	}

	/**
	 * 获得SockIO连接池
	 * @return SockIO连接池
	 */
	public final SockIOPool getSockIOPool() {
		return pool;
	}

	/**
	 * 获得MemCacheNeed
	 * @return MemCacheNeed
	 */
	public final MemCacheNeed getNeed() {
		return need;
	}

	/**
	 * 设置键值
	 * @param key 键
	 * @param value 值
	 */
	public final boolean set(String key, Object value) {
		return client.set(key, value);
	}

	/**
	 * 根据键获得值
	 * @param key 键
	 * @return 值
	 */
	public final Object get(String key) {
		return client.get(key);
	}
}
